package ru.spies.entities;

import static javax.persistence.CascadeType.ALL;
import static javax.persistence.CascadeType.MERGE;
import static javax.persistence.CascadeType.REFRESH;
import static javax.persistence.FetchType.EAGER;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;


/**
 * The persistent class for the agent database table.
 * 
 */
@Entity
@Table(name="agent")
@NamedQuery(name="Agent.findAll", query="SELECT a FROM Agent a")
public class Agent implements Serializable, ITableClass {
	private static final long serialVersionUID = 1L;

	private Object paramForFindQuery=null;
	
	@Id
	@GeneratedValue
	@Column(unique=true, nullable=false)
	private Integer id;

	@Column(name="count_recruits")
	private Integer countRecruits;

	@OneToOne(cascade = REFRESH, fetch = EAGER)
	@JoinColumn(name="id_recruiter")
	private Agent recruiter;

	@Column(nullable=false, length=40)
	private String name;

	private Boolean status;

	//bi-directional many-to-one association to Department
	@ManyToOne(fetch=EAGER, cascade = {REFRESH, MERGE})
	@JoinColumn(name="id_department")
	private Department department;

	//bi-directional many-to-one association to Agentsandtask
	@OneToMany(mappedBy="agent", fetch = EAGER, cascade = {REFRESH, MERGE})
	private List<Agentsandtask> agentsandtasks;
	
	//bi-directional many-to-one association to Siteuser
	@OneToOne(mappedBy="agent", fetch = EAGER, cascade = ALL)
	private Siteuser siteuser;
	
	//bi-directional many-to-one association to Department
	@OneToMany(mappedBy="agent", cascade = ALL, fetch = EAGER)
	private List<Document> documents;

	public Agent() {
	}

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getCountRecruits() {
		return this.countRecruits;
	}

	public void setCountRecruits(Integer countRecruits) {
		this.countRecruits = countRecruits;
	}

	public Agent getRecruiter() {
		return this.recruiter;
	}

	public void setRecruiter(Agent recruiter) {
		this.recruiter = recruiter;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Boolean getStatus() {
		return this.status;
	}

	public void setStatus(Boolean status) {
		this.status = status;
	}

	public Department getDepartment() {
		return this.department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public List<Agentsandtask> getAgentsandtasks() {
		return this.agentsandtasks;
	}

	public void setAgentsandtasks(List<Agentsandtask> agentsandtasks) {
		this.agentsandtasks = agentsandtasks;
	}

	public Agentsandtask addAgentsandtask(Agentsandtask agentsandtask) {
		getAgentsandtasks().add(agentsandtask);
		agentsandtask.setAgent(this);

		return agentsandtask;
	}

	public Agentsandtask removeAgentsandtask(Agentsandtask agentsandtask) {
		getAgentsandtasks().remove(agentsandtask);
		agentsandtask.setAgent(null);

		return agentsandtask;
	}
	
	public Siteuser getSiteuser() {
		return this.siteuser;
	}

	public void setSiteuser(Siteuser siteuser) {
		this.siteuser = siteuser;
	}
	
	public List<Document> getDocuments() {
		return this.documents;
	}

	public void setDocuments(List<Document> documents) {
		this.documents = documents;
	}

	public Document addDocument(Document document) {
		getDocuments().add(document);
		document.setAgent(this);

		return document;
	}

	@Override
	public void setParamForFindQuery(Object param) {
		paramForFindQuery=param;		
	}

	@Override
	public Object getParamForFindQuery() {
		return paramForFindQuery;
	}

	@Override
	public ITableClass createThisObject() {
		return new Agent();
	}

	@Override
	public String getQueryForFind() {
		return "SELECT x FROM Agent x WHERE x.id = :param";
	}
	
	public boolean isRegistered(){
		if (this.siteuser==null) return false;
		return true;
	}
	
}